/*
 * 版权所有 (C) 2001-2014 深圳市艾派应用系统有限公司。保留所有权利。
 * 版本：
 * 修改记录：
 *		1、2014-10-22，tfl创建。 
 */
package com.cmcc.aregister;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.Keyboard.Key;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;

public class KeyboardUtil
{

    @SuppressWarnings("unused")
    private Context ctx;
    @SuppressWarnings("unused")
    private Activity act;
    private KeyboardView keyboardView;
    // private Keyboard k1;// 字母键盘
    private Keyboard k2;// 数字键盘
    private EditText ed;

    public KeyboardUtil(Activity act, Context ctx, EditText edit)
    {
        this.act = act;
        this.ctx = ctx;
        this.ed = edit;
        k2 = new Keyboard(ctx, R.xml.symbols);

        keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view);
        initKeyboardView();
        keyboardView.setEnabled(true);
        keyboardView.setPreviewEnabled(true);
        keyboardView.setOnKeyboardActionListener(listener);
    }

    private OnKeyboardActionListener listener = new OnKeyboardActionListener() {
        @Override
        public void swipeUp()
        {
        }

        @Override
        public void swipeRight()
        {
        }

        @Override
        public void swipeLeft()
        {
        }

        @Override
        public void swipeDown()
        {
        }

        @Override
        public void onText(CharSequence text)
        {
        }

        @Override
        public void onRelease(int primaryCode)
        {
        }

        @Override
        public void onPress(int primaryCode)
        {
        }

        @Override
        public void onKey(int primaryCode, int[] keyCodes)
        {
            Editable editable = ed.getText();
            int start = ed.getSelectionStart();

            if (primaryCode == Keyboard.KEYCODE_CANCEL)
            {// 完成
                hideKeyboard();
            }
            else if (primaryCode == Keyboard.KEYCODE_DELETE)
            {// 回退
                if (editable != null && editable.length() > 0)
                {
                    if (start > 0)
                    {
                        editable.delete(start - 1, start);
                    }
                }
            }

            else if (primaryCode == 57419)
            { // go left
                if (start > 0)
                {
                    ed.setSelection(start - 1);
                }
            }
            else if (primaryCode == 57421)
            { // go right
                if (start < ed.length())
                {
                    ed.setSelection(start + 1);
                }
            }
            else
            {
                editable.insert(start, Character.toString((char) primaryCode));
            }
        }
    };

    private void initKeyboardView()
    {
        List<Key> keyList = k2.getKeys();
        // 查找出0-9的数字键
        List<Key> newkeyList = new ArrayList<Key>();
        for (int i = 0; i < keyList.size(); i++)
        {
            if (keyList.get(i).label != null
                && isNumber(keyList.get(i).label.toString()))
            {
                newkeyList.add(keyList.get(i));
            }
        }
        // 数组长度
        int count = newkeyList.size();
        // 结果集
        List<KeyModel> resultList = new ArrayList<KeyModel>();
        // 用一个LinkedList作为中介
        LinkedList<KeyModel> temp = new LinkedList<KeyModel>();
        // 初始化temp
        for (int i = 0; i < count; i++)
        {
            temp.add(new KeyModel(48 + i, i + ""));
        }
        // 取数
        Random rand = new Random();
        for (int i = 0; i < count; i++)
        {
            int num = rand.nextInt(count - i);
            resultList.add(new KeyModel(temp.get(num).getCode(), temp.get(num)
                .getLable()));
            temp.remove(num);
        }
        for (int i = 0; i < newkeyList.size(); i++)
        {
            newkeyList.get(i).label = resultList.get(i).getLable();
            newkeyList.get(i).codes[0] = resultList.get(i).getCode();
        }
        keyboardView.setKeyboard(k2);
    }

    public void showKeyboard()
    {
        int visibility = keyboardView.getVisibility();
        if (visibility == View.GONE || visibility == View.INVISIBLE)
        {

            keyboardView.setVisibility(View.VISIBLE);
        }
    }

    public void hideKeyboard()
    {
        int visibility = keyboardView.getVisibility();
        if (visibility == View.VISIBLE)
        {
            keyboardView.setVisibility(View.GONE);
        }
    }

    private boolean isNumber(String str)
    {
        String wordstr = "0123456789";
        if (wordstr.indexOf(str) > -1)
        {
            return true;
        }
        return false;
    }
}
